package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcDaysTimeConfigMapper;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.domain.OmcDaysTimeConfig;
import com.csun.cmny.provider.service.OmcDaysTimeConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;

@Slf4j
@Service
@Transactional
public class OmcDaysTimeConfigServiceImpl extends BaseService<OmcDaysTimeConfig> implements OmcDaysTimeConfigService {

    @Resource
    private OmcDaysTimeConfigMapper omcDaysTimeConfigMapper;

    @Override
    public Integer saveDaysTimeConfig(OmcDaysTimeConfig omcDaysTimeConfig, LoginAuthDto loginAuthDto) {

        omcDaysTimeConfig.setUpdateInfo(loginAuthDto);

        //校验同一个机构中只有一条配置数据
        //先查询该机构是否有该项配置
        OmcDaysTimeConfig daysTimeConfigQuery = new OmcDaysTimeConfig();
        daysTimeConfigQuery.setGroupId(omcDaysTimeConfig.getGroupId());
        daysTimeConfigQuery = selectOne(daysTimeConfigQuery);

        //如果存在则修改
        if(daysTimeConfigQuery != null){
            omcDaysTimeConfig.setId(daysTimeConfigQuery.getId());
            return omcDaysTimeConfigMapper.updateByPrimaryKeySelective(omcDaysTimeConfig);
        //不存在则插入
        }else{
            return omcDaysTimeConfigMapper.insertSelective(omcDaysTimeConfig);
        }
    }

    public boolean checkGroupId(OmcDaysTimeConfig omcDaysTimeConfig){
        Example example = new Example(OmcDaysTimeConfig.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("groupId",omcDaysTimeConfig.getGroupId());
        if(omcDaysTimeConfig.getId() != null){
            criteria.andNotEqualTo("id",omcDaysTimeConfig.getId());
        }
        int ret = selectCountByExample(example);
        return ret<1;
    }

}
